package tests;

import static org.junit.Assert.*;

import java.util.Vector;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import MyLinkedList.MyLinkedList;

public class MyLinkedListTests {
	private MyLinkedList<Integer> _l = null;
	
	@Before
	public void setUp() {
		_l = new MyLinkedList<Integer>();
	}
	
	@After
	public void tearDown() {
		_l = null;
	}

	@Test
	public void NewListIsEmpty() {		
		assertTrue(_l.isEmpty());
	}
	
	@Test
	public void OneElementList() {
		_l.add(0, 0);
		
		assertEquals(_l.size(), 1);
	}
	
	@Test
	public void InOrderElementsList() {
		int N = 20;
		Vector<Integer> elems = new Vector<Integer>();
		for(int i = 0; i < N; ++i)
			elems.add(i);
		
		int sizeVector = elems.size();
		for(int i = 0; i < sizeVector; ++i)
			_l.add(i, elems.elementAt(i));
		
		assertEquals(_l.size(), sizeVector);
		for(int i = 0; i < sizeVector; ++i)
			assertTrue(_l.get(i).equals(elems.elementAt(i)));
	}
	
	@Test
	public void DescOrderElementsList() {
		int N = 20;
		Vector<Integer> elems = new Vector<Integer>();
		for(int i = 0; i < N; ++i)
			elems.add(i);
		
		int sizeVector = elems.size();
		for(int i = 0; i < sizeVector; ++i)
			_l.add(i, elems.elementAt(sizeVector - i - 1));
		
		assertEquals(_l.size(), sizeVector);
		for(int i = 0; i < sizeVector; ++i)
			assertTrue(_l.get(i).equals(elems.elementAt(sizeVector - i - 1)));
	}
	
	@Test (expected=IndexOutOfBoundsException.class)
	public void EmptyListAccess() {
		_l.get(0);
	}
	
	@Test (expected=IndexOutOfBoundsException.class)
	public void NotEmptyListIllegalAccess() {
		int N = 20;
		Vector<Integer> elems = new Vector<Integer>();
		for(int i = 0; i < N; ++i)
			elems.add(i);
		
		int sizeVector = elems.size();
		for(int i = 0; i < sizeVector; ++i)
			_l.add(i, elems.elementAt(i));
		
		_l.get(sizeVector);
	}
	
	@Test
	public void DeleteOneElemFromList() {
		int N = 20;
		Vector<Integer> elems = new Vector<Integer>();
		for(int i = 0; i < N; ++i)
			elems.add(i);
		
		for(int i = 0; i < elems.size(); ++i)
			_l.add(i, elems.elementAt(i));
		
		
		int itemToDelete = N / 20;
		
		_l.remove(itemToDelete);
		elems.remove(itemToDelete);
		
		assertEquals(elems.size(), _l.size());
		for(int i = 0; i < elems.size(); ++i)
			assertTrue(_l.get(i).equals(elems.elementAt(i)));
	}
	
	@Test
	public void DeleteSomeElemFromList() {
		int N = 20;
		Vector<Integer> elems = new Vector<Integer>();
		for(int i = 0; i < N; ++i)
			elems.add(i);
		
		for(int i = 0; i < elems.size(); ++i)
			_l.add(i, elems.elementAt(i));
		
		
		int itemToDelete = N / 20;
		
		_l.remove(itemToDelete);
		_l.remove(itemToDelete/2);
		_l.remove(itemToDelete/2 + itemToDelete);
		
		elems.remove(itemToDelete);
		elems.remove(itemToDelete/2);
		elems.remove(itemToDelete/2 + itemToDelete);
		
		assertEquals(elems.size(), _l.size());
		for(int i = 0; i < elems.size(); ++i)
			assertTrue(_l.get(i).equals(elems.elementAt(i)));
	}
	

}
